package net.phpress.db;

import java.sql.*;

import javax.naming.*;
import javax.sql.DataSource;

public class DBConn {
	protected static Connection conn = null;
	protected static Statement stmt = null;
	protected static PreparedStatement pstmt = null;
	protected static ResultSet rs = null;

	protected static void Connection() {
		openConnection("PHDB");
	}

	protected static void openConnection(String pstrName) {
		try {
			Context ctx = new InitialContext();
			DataSource ds = (DataSource) ctx.lookup(pstrName);
			conn = ds.getConnection();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	protected static void closeConnection() throws SQLException {
		if (!conn.isClosed()) {
			conn.close();
		}
		if (!stmt.isClosed()) {
			stmt.close();
		}
		if (!pstmt.isClosed()) {
			pstmt.close();
		}
		if (!rs.isClosed()) {
			rs.close();
		}
		conn = null;
		stmt = null;
		pstmt = null;
		rs = null;
	}
}
